<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="操作员" prop="operatorName">
        <el-input
          v-model="queryParams.operatorName"
          placeholder="请输入操作员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认员" prop="confirmerName">
        <el-input
          v-model="queryParams.confirmerName"
          placeholder="请输入确认员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:catenary_switch_record:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:catenary_switch_record:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:catenary_switch_record:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:catenary_switch_record:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="catenary_switch_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <!--      <el-table-column label="操作员工号" align="center" prop="operatorWorkNumber" />-->
      <el-table-column label="开关名称" align="center" prop="switchName"  width="180"/>
      <el-table-column label="操作员姓名" align="center" prop="operatorName"  width="180"/>
      <!--      <el-table-column label="确认员工号" align="center" prop="confirmerWorkNumber" />-->
      <el-table-column label="确认员姓名" align="center" prop="confirmerName"  width="180"/>
      <!--      <el-table-column label="打开时间" align="center" prop="openTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.openTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="关闭时间" align="center" prop="closeTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="审核通过时间" align="center" prop="approvalTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作类型" align="center" prop="cmd"  width="180">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cmd === 'OPEN'">打开</el-tag>
          <el-tag type="danger" v-if="scope.row.cmd === 'CLOSE'">关闭</el-tag>
        </template>

      </el-table-column>
      <el-table-column label="已开启/关闭时长" align="center" prop="cmd" width="200">
        <template slot-scope="scope">
          <el-tag type="info">{{ formatTimeDifference(scope.row.openTime, scope.row.closeTime)}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作说明" align="left" prop="description">
        <template slot-scope="scope">
          <span style="color:#67C23A ">{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:catenary_switch_record:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:catenary_switch_record:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接触网开关记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id"/>
        </el-form-item>
        <el-form-item label="图纸ID" prop="drawingId">
          <el-input v-model="form.drawingId" placeholder="请输入图纸ID"/>
        </el-form-item>
        <el-form-item label="操作员ID" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作员ID"/>
        </el-form-item>
        <el-form-item label="操作员工号" prop="operatorWorkNumber">
          <el-input v-model="form.operatorWorkNumber" placeholder="请输入操作员工号"/>
        </el-form-item>
        <el-form-item label="操作员姓名" prop="operatorName">
          <el-input v-model="form.operatorName" placeholder="请输入操作员姓名"/>
        </el-form-item>
        <el-form-item label="确认员ID" prop="confirmerId">
          <el-input v-model="form.confirmerId" placeholder="请输入确认员ID"/>
        </el-form-item>
        <el-form-item label="确认员工号" prop="confirmerWorkNumber">
          <el-input v-model="form.confirmerWorkNumber" placeholder="请输入确认员工号"/>
        </el-form-item>
        <el-form-item label="确认员姓名" prop="confirmerName">
          <el-input v-model="form.confirmerName" placeholder="请输入确认员姓名"/>
        </el-form-item>
        <el-form-item label="打开时间" prop="openTime">
          <el-date-picker clearable
                          v-model="form.openTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择打开时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关闭时间" prop="closeTime">
          <el-date-picker clearable
                          v-model="form.closeTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择关闭时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核通过时间" prop="approvalTime">
          <el-date-picker clearable
                          v-model="form.approvalTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择审核通过时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前审核人ID" prop="currentApproverId">
          <el-input v-model="form.currentApproverId" placeholder="请输入当前审核人ID"/>
        </el-form-item>
        <el-form-item label="当前审核人工号" prop="currentApproverWorkNumber">
          <el-input v-model="form.currentApproverWorkNumber" placeholder="请输入当前审核人工号"/>
        </el-form-item>
        <el-form-item label="当前审核人姓名" prop="currentApproverName">
          <el-input v-model="form.currentApproverName" placeholder="请输入当前审核人姓名"/>
        </el-form-item>
        <el-form-item label="描述信息" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="标准参数" prop="standardParams">
          <el-input v-model="form.standardParams" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="自定义参数" prop="customParams">
          <el-input v-model="form.customParams" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="扩展信息" prop="ext">
          <el-input v-model="form.ext" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCatenary_switch_record,
  getCatenary_switch_record,
  delCatenary_switch_record,
  addCatenary_switch_record,
  updateCatenary_switch_record
} from "@/api/system/catenary_switch_record"

export default {
  name: "Catenary_switch_record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接触网开关记录表格数据
      catenary_switch_recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operatorName: null,
        confirmerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createBy: [
          {required: true, message: "创建者不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateBy: [
          {required: true, message: "更新者不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        deptId: [
          {required: true, message: "部门id不能为空", trigger: "blur"}
        ],
        drawingId: [
          {required: true, message: "图纸ID不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询接触网开关记录列表 */
    getList() {
      this.loading = true
      listCatenary_switch_record(this.queryParams).then(response => {
        this.catenary_switch_recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        drawingId: null,
        operatorId: null,
        operatorWorkNumber: null,
        operatorName: null,
        confirmerId: null,
        confirmerWorkNumber: null,
        confirmerName: null,
        openTime: null,
        closeTime: null,
        approvalTime: null,
        currentApproverId: null,
        currentApproverWorkNumber: null,
        currentApproverName: null,
        description: null,
        standardParams: null,
        customParams: null,
        ext: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加接触网开关记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCatenary_switch_record(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改接触网开关记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCatenary_switch_record(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCatenary_switch_record(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除接触网开关记录编号为"' + ids + '"的数据项？').then(function () {
        return delCatenary_switch_record(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/catenary_switch_record/export', {
        ...this.queryParams
      }, `catenary_switch_record_${new Date().getTime()}.xlsx`)
    },
    formatTimeDifference(startTime, endTime) {
      console.log(startTime,endTime)
      // 处理时间格式，确保兼容性
      const formatTime = (timeStr) => {
        return timeStr.replace(/-/g, '/').replace(' ', 'T') + 'Z';
      };

      try {
        // const start = new Date(formatTime(startTime));
        // const end = new Date(formatTime(endTime));
        const start = new Date(startTime.replace(/-/g, '/'));
        const end = new Date(endTime.replace(/-/g, '/'));
        console.log(start,end)
        if (isNaN(start.getTime()) || isNaN(end.getTime())) {

          throw new Error('时间格式不正确');
        }

        const diffMs = Math.abs(end - start);

        const hours = Math.floor(diffMs / (1000 * 60 * 60));
        const minutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((diffMs % (1000 * 60)) / 1000);

        // return {
        //   hours,
        //   minutes,
        //   seconds,
        //   totalMs: diffMs,
        //   formatted: `${hours}小时${minutes}分钟${seconds}秒`
        // };
        return `${hours}小时${minutes}分钟${seconds}秒`;
      } catch (error) {
        console.error('时间计算错误:', error.message);
        return startTime+','+endTime;
      }
    }
  }
}
</script>
